package im.agents.users;

import im.domain.SLA;
import im.domain.XMLSerializer;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

public class WaitForSLAConfirm extends Behaviour {
	private boolean done=false;
	private String convId;
	
	public WaitForSLAConfirm(Agent aid, String convId){
		super(aid);
		this.convId=convId;
	}
	@Override
	public void action() {
		ACLMessage confirm = myAgent.receive(
				MessageTemplate.MatchConversationId(convId));
		if(confirm!=null){
			if(confirm.getPerformative() == ACLMessage.REFUSE){
				System.out.println("SLA non sottoscritto");			
			}else if(confirm.getPerformative() == ACLMessage.INFORM){
				SLA sla = (SLA)XMLSerializer.doDeserialize(confirm.getContent());
				System.out.println("SLA sottoscritto: "+sla.toString());
				((Requester)myAgent).addSubscribedSLA(sla.toString(), sla);
				myAgent.addBehaviour(new HandleViolations(myAgent,sla));
			}
			
			done=true;
			
		}else{
			block();
		}
	}

	@Override
	public boolean done() {
		return done;
	}

}
